﻿var pageList={
	init : function(){
		var orderBy = $("[name=orderBy]").val();
		if(!orderBy) return true;

		var orderItemArray = orderBy.split(",");

		var $thead = $("thead", ".searchDataList");
		$.each(orderItemArray, function(i, val){
			var orderItem = val.split(" ");
			$thead.find("th.order[filed="+orderItem[0]+"]").addClass(orderItem[1]);
		});

		return true;
	},

	pageMove : function(){
		var pageNum = $(this).attr("pageNum");
		if(pageNum)	$(this).closest(".searchForm").find("[name=pageNum]").val(pageNum).end().submit();
		return false;
	},

	pageOrderby : function(){
		var $this = $(this);
		if($this.hasClass("DESC")){
			$this.removeClass("DESC").addClass("ASC");
		}else {
			$this.removeClass("ASC").addClass("DESC");
		};

		/**
		 * <th/> 클릭 시 order by 에 쿼리 넣어놓기
		 */
		var orderBy = "";
		$(this).closest("thead").find("th.order").each(function(i){
			var filed = $(this).attr("filed");
			if($(this).hasClass("DESC")){
				if(orderBy) orderBy+=",";
				orderBy += filed + " DESC";
			}else if($(this).hasClass("ASC")){
				if(orderBy) orderBy+=",";
				orderBy += filed + " ASC";
			}
		});

		$this.closest(".searchForm").find("[name=orderBy]").val(orderBy).end().submit();
		return false;
	}

};

$(function(){
	pageList.init();
	$("a", ".paginate").click(pageList.pageMove);
	$("select[name=pageListSize]").change(function(){
		$(this).closest(".searchForm").submit();
	});
	$("th.order", ".searchDataList").click(pageList.pageOrderby);
});
